How can I store a reference to `std::cout` as a class member?

Struct Output { static ostream& stream; }; ostream& Output::stream = cout; int main() { Output::stream.

That was my plan, but my internal const pirate tried to make it const, which wasn't possible, and in retrospect, unwanted by my design... Thanks for pointing me to the obvious. – rubenvb Feb 22 at 18:03 @rubenvb: I don't see much reason for such class. It is statically hardcoded at the beginning of the program to cout and you can't change it.

If you want it to be any other stream as you say, you need to recompile. So what's the use? It looks just as a synonym for cout the way it is here.

What if in the future you will want to, say, read an output file name from configuration and than to set your output to it? – davka Feb 22 at 18:24 1 @rubenvb: You can't reseat references. That's how references work.

Once you create a reference, it will always point to the same object. The reason they made C++ this way isn't clear, but one reason is that a reference is treated syntactically as the object it references, and therefore, a syntax like Foo& theRef = obj; theRef = anotherObj; should call the the operator=() of obj, rather than make theRef point to anotherObj. – Stefan Monov Feb 22 at 18:37 2 @Stefan: "The reason they made C++ this way isn't clear" - I don't think it's that unclear.It's because a reference was conceived as an alternative name for an object, just a variable name is a name for an object.

If I do int I = 23;, then the only way to make I refer to a different object is to shadow it in a smaller scope, although of course I can change the value of the object by assigning a new value. The same is true of a reference to an object, as is true of a variable name. Perhaps it's unclear why they're called "references", ISTR Stroustrup considered calling them "aliases".

– Steve Jessop Feb 22 at 18:45 1 ... after that initial conception, any resemblance that C++ references may bear to C++ pointers, or to pointers in Java (ahem, sorry, to references in Java), or different kinds of references in any other language, is coincidental. – Steve Jessop Feb 22 at 18:49.

Store it as std::ostream*. Sometimes people store references as members. This is error prone because the reference can not be reassigned, which causes assignment operator to do the wrong thing.

2 True: But the other side of the coin is that pointers can be NULL. You can get around the reference being non-re-assignable by using the boost reference objects. In my opinion always prefer reference until the point where you have no choice (you need to re-assign).

Also note: The assignment operators do not do the wrong thing (they will result in a compiler error). Which is what you want. – Loki Astari Feb 22 at 17:58 Sometimes the compiler will squawk in this case.

However, the OP is asking for a static reference which won't be copied. – mkb Feb 22 at 17:58 1 @Martin York: boost reference is a wrapped pointer. – Maxim Yegorushkin Feb 22 at 18:10 Yes.

The emphasis is on wrapped and not RAW. – Loki Astari Feb 22 at 19:10 It is like using a smart pointer where a plain pointer would suffice. Such an attitude leads to bloatware.

Just use Java already, lol. – Maxim Yegorushkin Feb 227 at 10:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions